//
//  STLinkageTable.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/10/25.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STLinkageTable.h"

@implementation STLinkageTable

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    tableFrame = frame;
    return self;
}

-(void)loadLinkageTableView{
    currentSelLeftNum = 0;
//    [self loadData];
    [self loadLeftTableview];
    [self loadRightTableview];
    [self lineView];
}

//-(void)loadData{
//   
//}

-(void)loadLeftTableview
{
    if (nil == leftTableview) {
        leftTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,100, self.frame.size.height)];
        leftTableview.backgroundColor = [UIColor groupTableViewBackgroundColor];
        leftTableview.rowHeight = 40;
        leftTableview.delegate = self;
        leftTableview.dataSource = self;
        leftTableview.tableFooterView = [UIView new];
        [self addSubview:leftTableview];
    }else{
        [leftTableview reloadData];
    }
}

-(void)loadRightTableview{
    if (nil == rightTableview) {
        rightTableview = [[UITableView alloc]initWithFrame:CGRectMake(100, 0, self.frame.size.width - 100, self.frame.size.height)];
        rightTableview.backgroundColor = [UIColor whiteColor];
        rightTableview.rowHeight = 40;
        rightTableview.delegate = self;
        rightTableview.dataSource = self;
        rightTableview.tableFooterView = [UIView new];
        [self addSubview:rightTableview];
    }else{
        [rightTableview reloadData];
    }
}

-(UIView *)lineView{
    if (nil == _lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(100, 0, 0.5, self.frame.size.height)];
        _lineView.backgroundColor = [UIColor blackColor];
        [self addSubview:_lineView];
    }
    return _lineView;
}

#pragma mark - 数据源方法

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView == leftTableview) {
        return 1;
    }else{
        return 1;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == leftTableview) {
        return _dataArray.count;
    }else{
        STCityInfoModel *item2 = [_dataArray objectAtIndex:currentSelLeftNum];
        return item2.areaList.count;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndentifier = @"cell";
    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIndentifier];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (tableView == leftTableview) {
        UIView *selectedBackGroundView = [[UIView alloc]initWithFrame:cell.frame];
        
        selectedBackGroundView.backgroundColor = [UIColor whiteColor];
        cell.backgroundColor = RGBACOLOR(217, 217, 217, 0.5);
        cell.selectedBackgroundView = selectedBackGroundView;
        if(indexPath.row ==currentSelLeftNum ){
            [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
            
//        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5,80)];
//        line.backgroundColor = [UIColor orangeColor];
        
//        [selectedBackGroundView addSubview:line];
        STCityInfoModel * model =_dataArray[indexPath.row];
        cell.textLabel.text = model.name;
        cell.textLabel.font = FONT(15);
    }else{
        STCityInfoModel * model =_dataArray[currentSelLeftNum];
        STCityInfoModel * model2= model.areaList[indexPath.row];
        cell.textLabel.text = model2.name;
        cell.textLabel.font = FONT(15);
    }
    return cell;
}

#pragma mark - 代理方法

////行高
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (tableView == leftTableview) {
//        return 80;
//    }else{
//        return 130;
//    }
//}

////头部高度
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    if (tableView == leftTableview) {
//        return 0;
//    }else{
//        return 30;
//    }
//}

////底部视图高度
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    if (tableView == leftTableview) {
//        return 0;
//    }else{
//        return CGFLOAT_MIN;
//    }
//}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    if (tableView == rightTableview) {
//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
//        view.backgroundColor = RGBACOLOR(217, 217, 217, 0.7);
//        UILabel *label = [[UILabel alloc]initWithFrame:view.bounds];
//        NSDictionary *item = [_dataArray objectAtIndex:section];
//        NSString *title = [item objectForKey:@"title"];
//        label.text = [NSString stringWithFormat:@"    %@",title];
//        [view addSubview:label];
//        return view;
//    }else{
//        return nil;
//    }
//}

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    if (_isRelate) {
        NSInteger topCellSection = [[[tableView indexPathsForVisibleRows]firstObject]section];
        
        if (tableView == rightTableview) {
            [leftTableview selectRowAtIndexPath:[NSIndexPath indexPathForItem:topCellSection inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
    }
}

-(void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section{
    if (_isRelate) {
        NSInteger topCellSection = [[[tableView indexPathsForVisibleRows]firstObject]section];
        if (tableView == rightTableview) {
            [leftTableview selectRowAtIndexPath:[NSIndexPath indexPathForItem:topCellSection inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSIndexPath * newIndexPath ;
    if (tableView == leftTableview) {
        newIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
        _isRelate = NO;
        [leftTableview selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        currentSelLeftNum = (int)indexPath.row;
        [rightTableview reloadData];
    }else{
        newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
    }
    if ([self.delegate respondsToSelector:@selector(linkageTableView:indexPath:)]) {
        [self.delegate linkageTableView:tableView indexPath:newIndexPath];
    }
    if ((tableView == leftTableview && indexPath.row>0)||tableView == rightTableview)  {
        if ([self.delegate respondsToSelector:@selector(linkageTableViewSelOver)]) {
            [self.delegate linkageTableViewSelOver];
        }
    }
    
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    _isRelate = YES;
}

@end
